package com.demo.command;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PythonCommand {
    public static void main(String[] args) throws IOException {

        List<String> cmdList = new ArrayList<>();
        cmdList.add("/bin/bash");
        cmdList.add("-c");
        cmdList.add(args[0]);
        ProcessBuilder processBuilder = new ProcessBuilder(cmdList);
        processBuilder.redirectErrorStream(true);
        Process processWork = processBuilder.start();
        BufferedReader jobOut = new BufferedReader(new InputStreamReader(processWork.getInputStream(), args[2]), 5000);

        PrintStream printStream = new PrintStream(new FileOutputStream(new File(args[1])), true, args[3]);
        String line;
        while ((line = jobOut.readLine()) != null) {
            printStream.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ": " + line);
            printStream.flush();
        }
        jobOut.close();
        printStream.close();
    }
}
